package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.query.CourseInfoSearch;
import com.atguigu.guli.service.edu.entity.query.CourseSearch;
import com.atguigu.guli.service.edu.entity.vo.CourseDetailsVo;
import com.atguigu.guli.service.edu.entity.vo.CourseInfoVo;
import com.atguigu.guli.service.edu.mapper.CourseDescriptionMapper;
import com.atguigu.guli.service.edu.mapper.CourseMapper;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-07-27
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    CourseDescriptionMapper courseDescriptionMapper;
    @Override
    public String saveCourseInfo(CourseInfoForm courseInfoForm) {

        Course course = new Course();
        BeanUtils.copyProperties(courseInfoForm,course);//将数据源对象的属性值设值给目标对象相同的属性
        baseMapper.insert(course);//将课程对象保存到数据库

        //课程简介 和课程数据是1对1的关系，为了方便 课程查找自己的简介
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(course.getId());
        courseDescriptionMapper.insert(courseDescription);

        return course.getId();
    }

    //更新课程基本信息
    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm, String courseId) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoForm,course);
        course.setId(courseId);
        baseMapper.updateById(course);

        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(courseId);
        courseDescriptionMapper.updateById(courseDescription);
    }

    //带条件查询分页数据
    @Override
    public void listPage(Page<CourseInfoVo> page, CourseInfoSearch courseInfoSearch) {
        String subjectId = courseInfoSearch.getSubjectId();
        String subjectParentId = courseInfoSearch.getSubjectParentId();
        String teacherId = courseInfoSearch.getTeacherId();
        String title = courseInfoSearch.getTitle();
        //查询的数据集合是CourseInfoVo对象的集合
        //可以自定义sql  将查询到的结果集封装为CourseInfoVo对象的集合
        //如果mapper的自定义方法第一个参数传入的是page对象，之前配置的分页拦截器会自动添加
        QueryWrapper<Course> queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(subjectId)){
            queryWrapper.eq("t1.subject_id",subjectId);
        }
        if (!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("t1.subject_parent_id",subjectParentId);
        }
        if (!StringUtils.isEmpty(teacherId)){
            queryWrapper.eq("t1.teacher_id",teacherId);
        }
        if (!StringUtils.isEmpty(title)){
            queryWrapper.like("t1.title",title);
        }

        queryWrapper.eq("1","1");

        List<CourseInfoVo> courseInfoVos = baseMapper.selectCourseInfoVoPage(page,queryWrapper);
        page.setRecords(courseInfoVos);
    }

    @Override
    public CourseInfoVo getCoursePublish(String courseId) {
        /*
            LambdaQueryWrapper: 只适用于单表的查询，指定自定义的条件 通过表对应的bean解析字段名
            如果使用了多表联查，联查的条件字段需要指定表必须使用QueryWrapper
         */
        QueryWrapper<Course> queryWrapper = new QueryWrapper();
        queryWrapper.eq("t1.id", courseId);
        return baseMapper.selectCourseInfoVoByCondition(queryWrapper);
    }

    @Override
    public List<Course> listCourses(CourseSearch courseSearch) {

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        String sortColumn = courseSearch.getSortColumn();
        String subjectId = courseSearch.getSubjectId();
        String subjectParentId = courseSearch.getSubjectParentId();
        Integer type = courseSearch.getType();
        queryWrapper.eq("status","Normal");
        if(!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("subject_parent_id" , subjectParentId);
        }
        if(!StringUtils.isEmpty(subjectId)){
            queryWrapper.eq("subject_id" , subjectId);
        }
        if(!StringUtils.isEmpty(sortColumn)){
            if(type==0){
                //升序
                queryWrapper.orderByAsc(sortColumn);
            }else{
                //降序
                queryWrapper.orderByDesc(sortColumn);
            }
        }
        return  baseMapper.selectList(queryWrapper);
    }

    @Override
    public CourseDetailsVo getCourseDetails(String courseId) {
        //更新课程的浏览数数量
        CourseDetailsVo courseDetailsVo = baseMapper.selectCourseDatilsVoByCourseId(courseId);
        courseDetailsVo.setViewCount(courseDetailsVo.getViewCount()+1);
        //将最新的浏览数量持久化到数据库
        Course course = new Course();
        course.setId(courseDetailsVo.getId());
        course.setViewCount(courseDetailsVo.getViewCount());
        baseMapper.updateById(course);
        return courseDetailsVo;
    }
}
